package com.itheima.controller;

import com.itheima.pojo.Address;
import com.itheima.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Xiao
 * @date 2021/9/14
 */
@Controller
public class BookController {

    //设置当前请求方法为POST，表示REST风格中的添加操作
    @RequestMapping(value = "/books",method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user) {
        System.out.println("BookController save user --> " + user);
        return "save";
    }

    //设置当前请求方法为DELETE，表示REST风格中的删除操作
    //@PathVariable注解用于设置路径变量（路径参数），
    // 要求路径上设置对应的占位符，并且占位符名称与方法形参名称相同
    @RequestMapping(value = "/books/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        System.out.println("BookController delete id --> " + id);
        return "delete";
    }

    //设置当前请求方法为PUT，表示REST风格中的修改操作
    @RequestMapping(value = "/books",method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user) {
        System.out.println("BookController update user --> " + user);
        return "update";
    }

    //设置当前请求方法为GET，表示REST风格中的查询操作
    @RequestMapping(value = "/books",method = RequestMethod.GET)
    @ResponseBody
    public String queryAll() {
        System.out.println("BookController queryAll...");
        return "queryAll";
    }

    //设置当前请求方法为GET，表示REST风格中的查询操作
    //@PathVariable注解用于设置路径变量（路径参数），
    // 要求路径上设置对应的占位符，并且占位符名称与方法形参名称相同
    @RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String queryById(@PathVariable("id") Integer bookId) {
        System.out.println("BookController queryById id --> " + bookId);
        return "queryById";
    }

    @RequestMapping("/books/findById")
    @ResponseBody
    public User findById(Integer id){
//        System.out.println(id);
        User user = new User();
        Address address = new Address();
        address.setProvince("湖南");
        address.setCity("长沙");
        user.setName("张三");
        user.setAge(23);
        user.setAddress(address);
//        user.setBirthday(new Date("2003-03-03"));   //IllegalArgumentException
        return user;
    }
}
